﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model_SSResourceManager;

namespace Presenter_SSResourceManager
{
    public class PresenterRegister
    {
        private readonly IViewRegister _viewRegister = null;
        private readonly Model_SSResourceManager.ModelsManager _mModel;
        public bool loginExist { get; set; }

        public PresenterRegister(IViewRegister view)
        {
            _viewRegister = view;
            _mModel = ModelInstanse.Get();
        }

        public void RegisterUser()
        {
            if (loginExist)
            {
                _viewRegister.ResultOfRegistration(resultValue: false);
            }
            else
            {
                var user = _mModel.userModel.Create();
                user = _viewRegister.GetUserInformation(user);
                bool ret = false;
                if (_mModel.userModel.Add(user))
                {
                    ret = true;
                    Model_SSResourceManager.Message _message = _mModel.messageModel.Create(user.Id, "Login " + user.Login.Trim() +
                        " has been successfully registered.\n To use Skype you need add < ssrm.administration > to your contact list");
                    _mModel.messageModel.Add(_message);
                }
                _viewRegister.ResultOfRegistration(resultValue: ret);
            }
        }

        public void LoginExist(string login)
        {
            loginExist = _mModel.userModel.CheckLoginExists(login);
            _viewRegister.LoginExist(loginExist);
        }

    }
}
